package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName: MyTask
 * Package:
 * Description
 * 自定义定时任务类
 *
 * @Author xw
 * @Create 2023/11/27 10:05
 */
@Component
@Slf4j
public class MyTask {

    /*
     * 订单
     * */
    @Autowired
    private OrderMapper orderMapper;

    /*
     * 变量记录调用次数
     * */
    private Integer i = 0;

    /*@Scheduled(cron = "0,5,10,15,20,25 * * * * ? ")
    public void executeTask() {
        i += 1;

        log.info("{}，时任务开始执行：{}", i, LocalDateTime.now());
    }*/



    /*
     * 0、查询 “支付超时” 下单时间-当前时间，超过15分钟的订单
     * */
    @Scheduled(cron = "0 * * * * ?")
    public void overtimePayment() {
        log.info("处理支付超时订单：{}", LocalDateTime.now());
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);

        Orders orders = new Orders();
        orders.setStatus(Orders.PENDING_PAYMENT); // 待支付
        orders.setOrderTime(time); // 下单时间

        // 调用订单持久层，查询订单状况
        List<Orders> list = orderMapper.checkOrderStatus(orders);


        if (list != null && list.size() > 0) {
            // 修改订单
            for (Orders i : list) {
                i.setStatus(i.CANCELLED); // 取消订单
                i.setCancelReason("支付超时！"); // 取消原因
                i.setCancelTime(LocalDateTime.now()); // 取消时间

                // 调用订单持久层，修改订单
                orderMapper.cancelId(i);
            }

        }

    }


    /*
     * 1、查询 “ 晚上12点 ” 前所以待支付订单，修改成取消订单
     * */
    @Scheduled(cron = "0 0 0 * * ?")
    public void night12() {

        LocalDateTime now = LocalDateTime.now();
        Orders orders = new Orders();
        orders.setOrderTime(now); // 下单时间
        orders.setStatus(Orders.PENDING_PAYMENT); // 待支付

        log.info("查询 “ 晚上12点 ” 前所以待支付订单，修改成取消订单：{}", now);

        // 调用订单持久层，查询订单状况
        List<Orders> list = orderMapper.checkOrderStatus(orders);
        if (list != null && list.size() > 0) {
            // 修改订单
            for (Orders i : list) {
                i.setStatus(i.CANCELLED); // 取消订单
                i.setCancelReason("店铺已打样！"); // 取消原因
                i.setCancelTime(now); // 取消时间

                // 调用订单持久层，修改订单
                orderMapper.cancelId(i);
            }
        }

    }


    /*
     * 2、查询 “ 晚上1点 ” 所以配送中订单，修改成已完成
     * */
    @Scheduled(cron = "0 0 1 * * ?")
    public void night01() {

        LocalDateTime now = LocalDateTime.now();
        Orders orders = new Orders();
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS); // 4派送中

        log.info("2、查询 “ 晚上1点 ” 所以配送中订单，修改成已完成：{}", now);

        // 调用订单持久层，查询订单状况
        List<Orders> list = orderMapper.checkOrderStatus(orders);

        if (list != null && list.size() > 0) {
            // 修改订单
            for (Orders i : list) {
                i.setStatus(i.COMPLETED); // 5已完成

                // 调用订单持久层，修改订单
                orderMapper.cancelId(i);
            }
        }

    }

}
